package com.happycat.manage;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

@Component
public class HappyCatAccessDecisionManager implements AccessDecisionManager {

	@Override
	public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
			throws AccessDeniedException, InsufficientAuthenticationException {
		 /**
         * 如果请求的资源不需要权限，则直接放行
         */
        if(configAttributes == null || configAttributes.size() <= 0) {
            return;
        }
        /**
         * 判断用户所拥有的权限是否是资源所需要的权限之一，如果是则放行，否则拦截
         */
		/*
		 * Iterator<ConfigAttribute> iter = configAttributes.iterator();
		 * while(iter.hasNext()) { String needRole = iter.next().getAttribute();
		 * for(GrantedAuthority grantRole : authentication.getAuthorities()) {
		 * if(grantRole.getAuthority().trim().equals(needRole)) { return; } } } throw
		 * new AccessDeniedException("权限不足，请前往配置权限");
		 */

	}

	@Override
	public boolean supports(ConfigAttribute attribute) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean supports(Class<?> clazz) {
		// TODO Auto-generated method stub
		return true;
	}

}
